package cyh.service.array;

import cyh.domain.dto.BaseDto;
import cyh.domain.dto.UserDto;
import cyh.domain.entity.Dept;
import cyh.domain.entity.Role;
import cyh.domain.entity.User;
import cyh.domain.vo.Result;
import cyh.domain.vo.UserVo;
import cyh.service.IDeptService;
import cyh.service.IRoleService;
import cyh.service.IUserService;



public class UserService extends  BaseService<User, UserDto, UserVo> implements IUserService {


    private final IDeptService deptService;
    private final IRoleService roleService;

    private static IUserService singleton;


    private UserService() {
        deptService =  DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATE_SIZE];
        temp = new UserVo[DATE_SIZE];

    }

    public static IUserService getInstance() {
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }

    User createEntity(UserDto dto, int id) {

        return new User(id,dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }
        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }
        return new UserVo(user.getId(), user.username, user.name, user.mobile, user.email, user.deptId, deptName, roleName, user.roleId, user.enabled);
    }


    @Override
    public  boolean roleInUse (int id){
        boolean inUse =false;
        for (User user:data)
        {
            if (user!=null &&user.roleId == id) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    @Override
    public  boolean deptInUse (int id){
        boolean inUse =false;
        for (User user:data)
        {
            if (user!=null &&user.deptId == id) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    @Override
    void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
    @Override
    public  Result create(UserDto dto) {

        if (!deptService.existById(dto.deptId))
            return Result.failure("创建失败，部门不存在");
        if (!roleService.existById(dto.roleId))
            return Result.failure("创建失败，角色不存在");
        return  super.create(dto);
    }
    @Override
    public  Result update(UserDto dto, int id) {
        if( !deptService.existById(dto.deptId))
            return Result.failure("更新失败，部门不存在");
        if (!roleService.existById(dto.roleId))
            return Result.failure("更新失败，角色不存在");
        return super.update(dto,id);
    }
    @Override
    public  Result toggle(int id) {
        if (!existById(id)) return Result.failure("更新失败，用户不存在");
        User  user = data[id];
        user.enabled = !user.enabled;
        return Result.success(data[id].enabled ? "启用成功" : "禁用成功", null);
    }


}